using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Linq;


namespace School_Manager
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        #region Menu Strip Items Show / Hide Panels

        private void termToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            pnNewTerm.Visible = true;
            pnlNewInstructor.Visible = false;
        }

        private void instructorToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            pnlNewInstructor.Visible = true;
            pnNewTerm.Visible = false;
        }

        private void assignmentToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            pnlAssignments.Visible = true;
            pnlNewClass.Visible = false;
        }

        private void classToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pnlAssignments.Visible = false;
            pnlNewClass.Visible = true;
        }

        private void newTermToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pnNewTerm.Visible = true;
        }

        private void instructorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pnlNewInstructor.Visible = true;
        }

        private void courseToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            pnlNewClass.Visible = true;
        }

        private void taskToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pnlNewClass.Visible = true;

        }

        private void termToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pnNewTerm.Visible = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            pnlAssignments.Visible = true;
            pnlNewClass.Visible = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            pnlAssignments.Visible = false;
            pnlNewClass.Visible = true;

        }

        private void button5_Click(object sender, EventArgs e)
        {
            pnlAssignments.Visible = false;
            pnlNewClass.Visible = false;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void btnGrades_Click(object sender, EventArgs e)
        {
            pnlClasses.Visible = false;
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            pnlClasses.Visible = true;
        }

        private void cmdClose_Click(object sender, EventArgs e)
        {
            pnlAssignments.Visible = false;
            pnlNewClass.Visible = false;
        }

        private void btnNewCourse_Click(object sender, EventArgs e)
        {

            pnlAssignments.Visible = false;
            pnlNewClass.Visible = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            pnlNewInstructor.Visible = false;
            pnNewTerm.Visible = true;
            
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pnlAssignments.Visible = true;
        }

        private void btnNewTermCancel_Click(object sender, EventArgs e)
        {
            pnNewTerm.Visible = false;
            pnlNewInstructor.Visible = true;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            pnlAssignments.Visible = false;
            pnlNewClass.Visible = false;
        }

        private void chkComplete_CheckedChanged(object sender, EventArgs e)
        {
            if (chkComplete.Checked)
                lblPercent.Visible = true;
                txtPercent.Visible = true;
        }

        private void btnNewCourse_Click_2(object sender, EventArgs e)
        {
            pnlNewClass.Visible = true;
        }

        private void cmdCancelAssignment_Click(object sender, EventArgs e)
        {
            pnlAssignments.Visible = false;
            pnlNewClass.Visible = true;
        }

        private void btnNewClassCancel_Click(object sender, EventArgs e)
        {
            pnlNewClass.Visible = false;
            pnlAssignments.Visible = true;
        }

        #endregion


        #region New records

        //New Term
        private void btnNewTermOk_Click(object sender, EventArgs e)
        {
            if (txtNewTermName.Text != null){
                SchoolManagerDataContext context = new SchoolManagerDataContext();
                tblTerm term = new tblTerm();
                term.EndDate = dtpTermTo.Value;
                term.StartDate = dtpTermFrom.Value;
                term.Term = txtNewTermName.Text;

                context.tblTerms.InsertOnSubmit(term);
                context.SubmitChanges();
                picCheckMark2.Visible = true;
            
            }
            txtNewTermName.Text = "";
        }

        //New Instructor
        private void btnNewInstructorOk_Click(object sender, EventArgs e)
        {
            if (txtNewTermName.Text != null)
            {
                SchoolManagerDataContext context = new SchoolManagerDataContext();
                tblInstructor instructor = new tblInstructor();

                instructor.FirstName = txtInstructorFirstName.Text;
                instructor.LastName = txtInstructorLastName.Text;
                instructor.Email = txtInstructorEmail.Text;
                instructor.PhoneNumber = txtInstructorPhoneNumber.Text;
                instructor.id = cboInstructorClass.SelectedIndex;

                context.tblInstructors.InsertOnSubmit(instructor);
                context.SubmitChanges();

                picNewInstructor.Visible = true;
            }
        // Clear all the txtboxes
        // Set pic visible to false

        }
        
        // New Class
        private void btnNewClass_Click(object sender, EventArgs e)
        {
            if (txtNewTermName.Text != null){
                SchoolManagerDataContext context = new SchoolManagerDataContext();
                tblClass oClass = new tblClass();
                oClass.Class = txtClassName.Text;
                oClass.Term = cboTerm.SelectedIndex;
                oClass.Instructor = cboInstructor.SelectedIndex;
                //oClass.Credit = nudCredits.Value;

                context.tblClasses.InsertOnSubmit(oClass);
                context.SubmitChanges();

                picNewClassCheck.Visible = true;
            }
        //Clear textboxes
        // set pic visibility to false
        }

        // New Assignment
        private void cmdNewAssignment_Click(object sender, EventArgs e)
        {
            if (txtAssignment.Text != null)
            {
                SchoolManagerDataContext context = new SchoolManagerDataContext();
                tblAssignment oAssignment = new tblAssignment();
                oAssignment.Assignment = txtAssignment.Text;
                oAssignment.Class = cboClass.SelectedIndex;
                oAssignment.Kind = cboKind.SelectedIndex;
                oAssignment.Priority = cboPriority.SelectedIndex;
                oAssignment.StartDate = dtpStartDate.Value;
                oAssignment.DueDate = dtpEndDate.Value;
                //oAssignment.Percent = txtPercent.Text;

                context.tblAssignments.InsertOnSubmit(oAssignment);
                context.SubmitChanges();
                picCheckMark2.Visible = true;
            }
            //Clear textboxes
            // set pic visibility to false
            dgvAllAssignments.Refresh();
        }
        
        #endregion
        

        // something Clear
        private void button3_Click_1(object sender, EventArgs e)
        {
            txtAssignment.Text = "";
            cboClass.Items.Clear();
            cboKind.Items.Clear();
            cboPriority.Items.Clear();
            chkComplete.Checked = false;
            lblPercent.Visible = false;
            txtPercent.Visible = false;

            picAssignment.Visible = false;
        }


        # region Form Load
        private void frmMain_Load(object sender, EventArgs e)
        {

        SchoolManagerDataContext context = new SchoolManagerDataContext();
                
            #region Fills Instructor name to cboInstructorClass
                        cboInstructorClass.DisplayMember = "Class";
                        cboInstructorClass.ValueMember = "id";

                        foreach (tblClass oClass in context.tblClasses)
                        {
                            cboInstructorClass.Items.Add(oClass);
                        }
            #endregion


            //#region Fills Instructor name to cboGradeAssignment
            //            cboGradeAssignments.DisplayMember = "Class";
            //            cboGradeAssignments.ValueMember = "id";

            //            foreach (tblAssignment assignment in context.tblAssignments)
            //            {
            //                cboGradeAssignments.Items.Add(assignment);
            //            }

                        
            //#endregion            


            #region Fills list of classes to cboClass
                        cboClass.DisplayMember = "Class";
                        cboClass.ValueMember = "id";

                        foreach (tblClass oClass in context.tblClasses)
                        {
                            cboClass.Items.Add(oClass);
                        }
            #endregion


            #region Fills kind to cboKind
                cboKind.DisplayMember = "Kind";
                cboKind.ValueMember = "id";

                foreach (tblKind kind in context.tblKinds)
                {
                    cboKind.Items.Add(kind);
                }
            #endregion


            #region Fills term to new cboTerm
                cboTerm.DisplayMember = "Term";
                cboTerm.ValueMember = "id";

                foreach (tblTerm term in context.tblTerms)
                {
                    cboTerm.Items.Add(term);
                }
            #endregion


            #region Fills priority to cboPriority
                cboPriority.DisplayMember = "Priority";
                cboPriority.ValueMember = "id";

                foreach (tblPriority priority in context.tblPriorities)
                {
                    cboPriority.Items.Add(priority);
                }
            #endregion


            #region Fills instructor to cboInstructor
                cboInstructor.DisplayMember = "LastName";
                cboInstructor.ValueMember = "id";

                foreach (tblInstructor instructor in context.tblInstructors)
                {
                    cboInstructor.Items.Add(instructor);
                }
            #endregion


            #region Fills lstAssignments
                lstAssignments.DisplayMember = "Assignment";
                lstAssignments.ValueMember  = "id";

                foreach (tblAssignment assignment in context.tblAssignments)
                {
                    lstAssignments.Items.Add(assignment);
                }
            #endregion

           // dgvClasses.DataSource
                var classes = from oClass in context.tblClasses
                              select oClass;
                dgvClasses.DataSource = classes;


                var oAssignments = from assignments in context.tblAssignments
                                  select assignments;
                dgvAllAssignments.DataSource = oAssignments;
        }

        #endregion

        private void fillByToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.tblClassTableAdapter.Fill(this.schoolManagerDataSet.tblClass);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }


    }
}